home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Devices and Hardware / CD ROM / CDROMSample / CDROMSample.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-10  |  14.0 KB  |  290 lines  |  [????/????]

  1. /*
  2.     File:        CDROMSample.c
  3.     
  4.     Description:    This sample demonstrates how to use IOKitLib to find CD-ROM media mounted on the             system. It also shows how to open, read raw sectors from, and close the drive.
  5.                 
  6.     Author:        gc
  7.  
  8.     Copyright:     © Copyright 2000 Apple Computer, Inc. All rights reserved.
  9.     
  10.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  11.                         ("Apple") in consideration of your agreement to the following terms, and your
  12.                         use, installation, modification or redistribution of this Apple software
  13.                         constitutes acceptance of these terms.  If you do not agree with these terms,
  14.                         please do not use, install, modify or redistribute this Apple software.
  15.  
  16.                         In consideration of your agreement to abide by the following terms, and subject
  17.                         to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  18.                         copyrights in this original Apple software (the "Apple Software"), to use,
  19.                         reproduce, modify and redistribute the Apple Software, with or without
  20.                         modifications, in source and/or binary forms; provided that if you redistribute
  21.                         the Apple Software in its entirety and without modifications, you must retain
  22.                         this notice and the following text and disclaimers in all such redistributions of
  23.                         the Apple Software.  Neither the name, trademarks, service marks or logos of
  24.                         Apple Computer, Inc. may be used to endorse or promote products derived from the
  25.                         Apple Software without specific prior written permission from Apple.  Except as
  26.                         expressly stated in this notice, no other rights or licenses, express or implied,
  27.                         are granted by Apple herein, including but not limited to any patent rights that
  28.                         may be infringed by your derivative works or by other works in which the Apple
  29.                         Software may be incorporated.
  30.  
  31.                         The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  32.                         WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  33.                         WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.                         PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  35.                         COMBINATION WITH YOUR PRODUCTS.
  36.  
  37.                         IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  38.                         CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  39.                         GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40.                         ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  41.                         OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  42.                         (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  43.                         ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44.                 
  45.     Change History (most recent first):
  46.         
  47.             <1>         08/16/00    gc        New sample.
  48.         
  49. */
  50.  
  51. #include <stdio.h>
  52. #include <string.h>
  53. #include <unistd.h>
  54. #include <fcntl.h>
  55. #include <sys/ioctl.h>
  56. #include <dev/disk.h>
  57. #include <errno.h>
  58. #include <paths.h>
  59. #include <sys/param.h>
  60. #include <IOKit/IOKitLib.h>
  61. #include <IOKit/IOBSD.h>
  62. #include <IOKit/storage/IOMedia.h>
  63. //#include <IOKit/storage/IOCDMedia.h>
  64. // the above header is currently missing: this will be fixed in the future
  65. #include <CoreFoundation/CoreFoundation.h>
  66.  
  67. #define kIOCDMediaClass    "IOCDMedia"    // this should be defined in a header somewhere but isn't currently
  68. #define kIOMediaClass    "IOMedia"    // this should be defined in a header somewhere but isn't currently
  69.  
  70. enum {
  71.     kCDSectorSizeWhole = 2352     // this should be defined in a header somewhere but isn't currently
  72. };
  73.  
  74. static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
  75. static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
  76. static int OpenDrive( const char *bsdPath );
  77. static Boolean ReadSector( int fileDescriptor );
  78. static void CloseDrive( int fileDescriptor );
  79.  
  80. // Returns an iterator across all CD media (class IOCDMedia). Caller is responsible for releasing
  81. // the iterator when iteration is complete.
  82. kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
  83. {
  84.     kern_return_t        kernResult; 
  85.     mach_port_t            masterPort;
  86.     CFMutableDictionaryRef    classesToMatch;
  87.  
  88. /*! @function IOMasterPort
  89.     @abstract Returns the mach port used to initiate communication with IOKit.
  90.     @discussion Functions that don't specify an existing object require the IOKit master port to be passed. This function obtains that port.
  91.     @param size The task's bootstrap port, pass bootstrap_port from mach/mach_init.h.
  92.     @param masterPort The master port is returned.
  93.     @result A kern_return_t error code. */
  94.  
  95.     kernResult = IOMasterPort( bootstrap_port, &masterPort );
  96.     if ( KERN_SUCCESS != kernResult )
  97.         printf( "IOMasterPort returned %d\n", kernResult );
  98.         
  99. /*! @function IOServiceMatching
  100.     @abstract Create a matching dictionary that specifies an IOService class match.
  101.     @discussion A very common matching criteria for IOService is based on its class. IOServiceMatching will create a matching dictionary that specifies any IOService of a class, or its subclasses. The class is specified by C-string name.
  102.     @param name The class name, as a const C-string. Class matching is successful on IOService's of this class or any subclass.
  103.     @result The matching dictionary created, is returned on success, or zero on failure. The dictionary is commonly passed to IOServiceGetMatchingServices or IOServiceAddNotification which will consume a reference, otherwise it should be released with CFRelease by the caller. */
  104.  
  105.     // CD media are instances of class kIOCDMediaClass
  106.     classesToMatch = IOServiceMatching( kIOCDMediaClass ); 
  107.     if ( classesToMatch == NULL )
  108.         printf( "IOServiceMatching returned a NULL dictionary.\n" );
  109.     else {
  110. /*!
  111.     @function CFDictionarySetValue
  112.     Sets the value of the key in the dictionary.
  113.     @param theDict The dictionary to which the value is to be set. If this
  114.         parameter is not a valid mutable CFDictionary, the behavior is
  115.         undefined. If the dictionary is a fixed-capacity dictionary and
  116.         it is full before this operation, and the key does not exist in
  117.         the dictionary, the behavior is undefined.
  118.     @param key The key of the value to set into the dictionary. If a key 
  119.         which matches this key is already present in the dictionary, only
  120.         the value is changed ("add if absent, replace if present"). If
  121.         no key matches the given key, the key-value pair is added to the
  122.         dictionary. If added, the key is retained by the dictionary,
  123.         using the retain callback provided
  124.         when the dictionary was created. If the key is not of the sort
  125.         expected by the key retain callback, the behavior is undefined.
  126.     @param value The value to add to or replace into the dictionary. The value
  127.         is retained by the dictionary using the retain callback provided
  128.         when the dictionary was created, and the previous value if any is
  129.         released. If the value is not of the sort expected by the
  130.         retain or release callbacks, the behavior is undefined.
  131. */
  132.  
  133.     CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectable ), kCFBooleanTrue ); 
  134.         // Each IOMedia object has a property with key kIOMediaEjectable which is true if the media
  135.         // is indeed ejectable. So add this property to the CFDictionary we're matching on. 
  136.     }
  137.     
  138.     /*! @function IOServiceGetMatchingServices
  139.         @abstract Look up registered IOService objects that match a matching dictionary.
  140.         @discussion This is the preferred method of finding IOService objects currently registered by IOKit. IOServiceAddNotification can also supply this information and install a notification of new IOServices. The matching information used in the matching dictionary may vary depending on the class of service being looked up.
  141.         @param masterPort The master port obtained from IOMasterPort().
  142.         @param matching A CF dictionary containing matching information, of which one reference is consumed by this function. IOKitLib can contruct matching dictionaries for common criteria with helper functions such as IOServiceMatching, IOOpenFirmwarePathMatching.
  143.         @param existing An iterator handle is returned on success, and should be released by the caller when the iteration is finished.
  144.         @result A kern_return_t error code. */
  145.  
  146.     kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );    
  147.     if ( KERN_SUCCESS != kernResult )
  148.         printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
  149.         
  150.     return kernResult;
  151. }
  152.     
  153. // Given an iterator across a set of CD media, return the BSD path to the
  154. // next one. If none are found the path name is set to an empty string.
  155. kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
  156. {
  157.     io_object_t        nextMedia;
  158.     kern_return_t    kernResult = KERN_FAILURE;
  159.     
  160.     /*! @function IOIteratorNext
  161.     @abstract Returns the next object in an iteration.
  162.     @discussion This function returns the next object in an iteration, or zero if no more remain or the iterator is invalid.
  163.     @param iterator An IOKit iterator handle.
  164.     @result If the iterator handle is valid, the next element in the iteration is returned, otherwise zero is returned. */
  165.  
  166.     nextMedia = IOIteratorNext( mediaIterator );
  167.     if ( nextMedia == NULL )
  168.     {
  169.         *bsdPath = '\0';
  170.     }
  171.     else {
  172.         CFTypeRef    bsdPathAsCFString;
  173.         
  174. /*! @function IORegistryEntryCreateCFProperty
  175.     @abstract Create a CF representation of a registry entry's property.
  176.     @discussion This function creates an instantaneous snapshot of a registry entry property, creating a CF container analogue in the caller's task. Not every object available in the kernel is represented as a CF container; currently OSDictionary, OSArray, OSSet, OSSymbol, OSString, OSData, OSNumber, OSBoolean are created as their CF counterparts. 
  177.     @param entry The registry entry handle whose property to copy.
  178.     @param key A CFString specifying the property name.
  179.     @param allocator The CF allocator to use when creating the CF container.
  180.     @param options No options are currently defined.
  181.     @result A CF container is created and returned the caller on success. The caller should release with CFRelease. */
  182.         bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, 
  183.                                                              CFSTR( kIOBSDName ), 
  184.                                                              kCFAllocatorDefault, 
  185.                                                              0 );
  186.  
  187.         *bsdPath = '\0';
  188.         if ( bsdPathAsCFString )
  189.         {
  190.             size_t devPathLength = strlen( _PATH_DEV );
  191.             
  192.             strcpy( bsdPath, _PATH_DEV );
  193.             
  194.             if ( CFStringGetCString( bsdPathAsCFString,
  195.                                      bsdPath + devPathLength,
  196.                                      maxPathSize - devPathLength, 
  197.                                      kCFStringEncodingASCII ) )
  198.             {
  199.                 printf( "BSD path: %s\n", bsdPath );
  200.                 kernResult = KERN_SUCCESS;
  201.             }
  202.             
  203.             CFRelease( bsdPathAsCFString );
  204.         }
  205.     }
  206.     
  207. /*! @function IOObjectRelease
  208.     @abstract Releases an object handle previously returned by IOKitLib.
  209.     @discussion All objects returned by IOKitLib should be released with this function when access to them is no longer needed. Using the object after it has been released may or may not return an error, depending on how many references the task has to the same object in the kernel.
  210.     @param object The IOKit object to release.
  211.     @result A kern_return_t error code. */
  212.     
  213.     IOObjectRelease( nextMedia );
  214.     
  215.     return kernResult;
  216. }
  217.  
  218. // Given the path to a CD drive, open the drive.
  219. // Return the file descriptor associated with the device.
  220. int OpenDrive( const char *bsdPath )
  221. {
  222.     int    fileDescriptor;
  223.     
  224.     fileDescriptor = open( bsdPath, O_RDONLY );
  225.     
  226.     if ( fileDescriptor == -1 )
  227.         printf( "Error %d opening device %s.\n", errno, bsdPath );
  228.     
  229.     return fileDescriptor;
  230. }
  231.  
  232. // Given the file descriptor for a whole-media CD device, read a sector drive.
  233. // Return true if successful, otherwise false.
  234. Boolean ReadSector( int fileDescriptor )
  235. {
  236.     char    buffer[ kCDSectorSizeWhole ];
  237.     ssize_t    numBytes;
  238.     
  239.     numBytes = read( fileDescriptor, buffer, kCDSectorSizeWhole );
  240.         
  241.     return numBytes == kCDSectorSizeWhole ? true : false;
  242. }
  243.  
  244. // Given the file descriptor for a device, close that device.
  245. void CloseDrive( int fileDescriptor )
  246. {
  247.     close( fileDescriptor );
  248. }
  249.  
  250. int main( int argc, char *argv[] )
  251. {
  252.     kern_return_t    kernResult; // on PowerPC this is an int (4 bytes)
  253. /*
  254.  *    error number layout as follows (see mach/error.h):
  255.  *
  256.  *    hi                        lo
  257.  *    | system(6) | subsystem(12) | code(14) |
  258.  */
  259.  
  260.     io_iterator_t    mediaIterator;
  261.     char        bsdPath[ MAXPATHLEN ];
  262.  
  263.     kernResult = FindEjectableCDMedia( &mediaIterator );
  264.     
  265.     kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
  266.     
  267.     // Now open the device we found, read a sector, and close the device
  268.     if ( bsdPath[ 0 ] != '\0' )
  269.     {
  270.         int fileDescriptor;
  271.         
  272.         fileDescriptor = OpenDrive( bsdPath );
  273.         
  274.         if ( ReadSector( fileDescriptor ) )
  275.             printf( "Sector read successfully.\n" );
  276.         else
  277.             printf( "Could not read sector.\n" );
  278.             
  279.         CloseDrive( fileDescriptor );
  280.         printf( "Device closed.\n" );
  281.     }
  282.     else
  283.         printf( "No ejectable CD media found.\n" );
  284.  
  285.     // Release the iterator.
  286.     IOObjectRelease( mediaIterator );
  287.         
  288.     return 0;
  289. }
  290.